<?php

class Post{

	public $data = array();
	public $id	 = '';
	
	/* 数据库参数 */
	protected $db;
	protected $db_table = 'post';
	
	/**
	* 单元常量(引用自全局define变量)
	* @var integer 
	*/
	private $unit = '';
	
	/**
	 * @param $unit 所属单元
	 * @return 
	 */
	public function __construct(){
		//$this->unit = $unit;
		$this->db = new DB();
		//获取数据库表名
		$this->db_table = $this->db->getTableName($this->db_table);
	}
	
	/**
	 * 添加文章
	 * @param $data	数据 
	 * @return 
	 */
	public function addPost($data){
		try{
			$sql = "INSERT INTO `$this->db_table` 
				(`r_title`, `r_content`, `r_state`, `r_index`, `r_crd_date`, `c_id`) 
				VALUES 
				('$data[title]','$data[content]','1','0',NOW(),'$data[cid]')";
			//TODO: 单元测试,前台菜单
			debug($sql);
			$this->db->Insert($sql);
			return true;
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
	 * 删除文章
	 * @param $id	分类id 
	 * @return 
	 */
	public function deletePost($id){
		try{
			$sql = "DELETE FROM `$this->db_table` WHERE `r_id` = '$id'";
			$this->db->delete($sql);
			return true;
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
	 * 更新文章
	 * @param $id	分类id
	 * @param $data	数据 
	 * @return 
	 */
	public function updatePost($id,$data){
		try{
			$sql = "UPDATE `$this->db_table` 
				 SET `r_title` =  '$data[title]',
				`r_content` =  '$data[content]',
				`r_state` =  '1',
				`r_index` =  '0',
				`r_upd_date` =  NOW(),
				`c_id` =  '$data[cid]' WHERE `$this->db_table`.`c_id` = $id";
			$this->db->update($sql);
			return true;
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
	
	/**
	 * 查询文章列表 或 单独文章内容
	 * @param ($id)	分类id (可选参数)
	 * @return 
	 */
	public function selectPost($id){
		try{
			if(empty($id)){
				$sql = "SELECT * FROM `$this->db_table` NATURAL JOIN `category` WHERE `$this->db_table`.`c_id` = `category`.`c_id`";
			}else{
				$sql = "SELECT * FROM `$this->db_table` NATURAL JOIN `category` WHERE `r_id` = '$id'";
			}
			$r = $this->db->select($sql);
			return $r;
		}catch (Exception $e){
			echo $e->getMessage();
		}
	}
}
/**
 * 文章分类
 * 
 * 继承逻辑分类
 */
class CmsPost extends Post{
	public function __construct(){
		parent::__construct();
	}
}
?>